#' ---
#' title: Replication Materials for "Hometown Advantage: Voter Preferences for Community Embeddedness in Local Contests" (Ornstein, Heideman, Moy, & Schiff, 2024)
#' date: 2024-09-17
#' version: 1.1
#' ---

library(tidyverse)
library(haven)
library(estimatr)
library(marginaleffects)
library(modelsummary)
library(cregg)

## Load and Clean Dataset ------------------------

load('CES2022_UGA.RData')

# to estimate AMCE with linear regression, make the candidate the unit of analysis
d <- d |> 
  mutate(contest_id = 1:n())

candidate1 <- d |> 
  mutate(candidate_selected = as.numeric(conjoint_choice == 1)) |> 
  select(caseid, contest_id, candidate_selected, 
         Age = A_Age, CareerHistory = A_CareerHistory,
         CommunityTies = A_CommunityTies, Name = A_Name,
         Race = A_Race, Gender = A_Gender, Political = A_Political,
         Endorsements = A_Endorsements, Family = A_Family,
         pid3, race, urbancity, residency = CC22_361, gender4, ownhome)

candidate2 <- d |> 
  mutate(candidate_selected = as.numeric(conjoint_choice == 2)) |> 
  select(caseid, contest_id, candidate_selected, 
         Age = B_Age, CareerHistory = B_CareerHistory,
         CommunityTies = B_CommunityTies, Name = B_Name,
         Race = B_Race, Gender = B_Gender, Political = B_Political,
         Endorsements = B_Endorsements, Family = B_Family,
         pid3, race, urbancity, residency = CC22_361, gender4, ownhome)

d <- bind_rows(candidate1, candidate2)

# convert attribute variables to factors
d <- d |> 
  mutate(Homeowner = factor(ifelse(str_detect(CommunityTies, 'owned a home'),
                                   'Homeowner', 'Renter'),
                            levels = c('Renter', 'Homeowner')),
         OldTimer = factor(ifelse(str_detect(CommunityTies, 'past 10 years'), 
                                  'Lived In Community 10 Years',
                                  'Lived In Community 2 Years'),
                           levels = c('Lived In Community 2 Years',
                                      'Lived In Community 10 Years')),
         # make "no endorsements" the base category
         Endorsements = factor(Endorsements) |> 
           fct_relevel('No endorsements'),
         # make "single" the base category for family
         Family = factor(Family) |> 
           fct_relevel('Single'),
         Age = factor(Age),
         CareerHistory = factor(CareerHistory),
         Political = factor(Political),
         Family = factor(Family),
         Gender = factor(Gender),
         Race = factor(case_when(Race == 'B' ~ 'Black',
                                 Race == 'L' ~ 'Latino',
                                 Race == 'W' ~ 'White')))



## Estimate AMCE ----------------------


fmla <- candidate_selected ~ Gender + Race + CareerHistory + 
  Homeowner + OldTimer + Age + Political + Family + Endorsements

amce <- lm_robust(fmla,
                  data = d,
                  cluster = caseid)


## Figure 2: AMCEs By Candidate Attribute ----------------------

# create list of figure labels
cm <- c('GenderMale' = 'Male',
        'RaceLatino' = 'Latino', 
        'RaceWhite' = 'White',
        'CareerHistoryConstruction worker' = 'Construction worker',
        'CareerHistoryHigh school teacher' = 'High school teacher',
        'CareerHistoryLocal attorney' = 'Local attorney',
        'CareerHistoryNot employed, taking care of home/family' = 'Not employed',
        'CareerHistoryPolice officer' = 'Police officer',
        'CareerHistoryReal estate developer' = 'Real estate developer',
        'HomeownerHomeowner' = 'Homeowner',
        'OldTimerLived In Community 10 Years' = 'Lived In Community 10 Years',
        'Age45' = 'Age 45',
        'Age60' = 'Age 60',
        'PoliticalPreviously elected to local office' = 'Previously elected to local office',
        'FamilyMarried with no children' = 'Married (no children)',
        'FamilyMarried with two children' = 'Married (two children)',
        'EndorsementsAn association of local real estate developers' = 'Real Estate Developer Assn',
        'EndorsementsLocal newspaper' = 'Local Newspaper',
        'EndorsementsThe county chamber of commerce' = 'Chamber of Commerce',
        'EndorsementsThe police union' = 'Police Union',
        "EndorsementsThe teachers' union" = 'Teachers Union')

modelplot(amce, coef_omit = 'Interc', coef_map = cm) + #, conf_level = 1-0.05/14) + 
  geom_vline(xintercept = 0, linetype = 'dashed') +
  labs(x = 'AMCE', color = '') +
  # facet by variable group
  facet_wrap(~if_else(term %in% c('Male', 'Latino', 'White', 'Age 45', 'Age 60'), 'Demographics',
                      if_else(term %in% c('Construction worker', 'High school teacher', 
                                          'Local attorney', 'Not employed','Police officer',
                                          'Real estate developer', 'Previously elected to local office'), 'Experience',
                              if_else(term %in% c('Homeowner', 'Lived In Community 10 Years', 
                                                  'Married (two children)', 'Married (no children)'), 
                                      'Personal Life',
                                      'Endorsements'))),
             scales = 'free_y') +
  theme_bw()

ggsave('fig_2.png', width = 8, height = 5)

## Figure 3: Conditional Effects By Homeownership ----------------

# conditional AMCE by homeownership
amce_homeowner <- lm_robust(fmla,
                            data = d |> filter(ownhome == 1),
                            cluster = caseid)

amce_renter <- lm_robust(fmla,
                         data = d |> filter(ownhome == 2),
                         cluster = caseid)

modelplot(models = list('Homeowner' = amce_homeowner,
                        'Renter' = amce_renter),
          coef_omit = 'Inter', coef_map = cm) +
  scale_color_manual(values = c('black', 'red')) + 
  geom_vline(xintercept = 0, linetype = 'dashed') +
  labs(x = 'AMCE', color = '') +
  # facet by variable group
  facet_wrap(~if_else(term %in% c('Male', 'Latino', 'White', 'Age 45', 'Age 60'), 'Demographics',
                      if_else(term %in% c('Construction worker', 'High school teacher', 
                                          'Local attorney', 'Not employed','Police officer',
                                          'Real estate developer', 'Previously elected to local office'), 'Experience',
                              if_else(term %in% c('Homeowner', 'Lived In Community 10 Years', 
                                                  'Married (two children)', 'Married (no children)'), 
                                      'Personal Life',
                                      'Endorsements'))),
             scales = 'free_y') +
  theme_bw() +
  theme(legend.position = 'bottom')

ggsave('fig_3.png', width = 8, height = 5)

## Figure 4: Conditional Effects By Political Party --------------

# Republican respondents
amce_republican <- lm_robust(fmla,
                             data = d |> filter(pid3 == 2),
                             cluster = caseid)

# Democratic respondents
amce_democratic <- lm_robust(fmla,
                             data = d |> filter(pid3 == 1),
                             cluster = caseid)

democratic_blue <- "#2E74C0"
republican_red <- "#CB454A"

modelplot(models = list('Democratic Respondents' = amce_democratic,
                        'Republican Respondents' = amce_republican),
          coef_omit = 'Inter', coef_map = cm) +
  scale_color_manual(values = c(democratic_blue, republican_red)) +
  geom_vline(xintercept = 0, linetype = 'dashed') +
  labs(x = 'AMCE', color = '') +
  # facet by variable group
  facet_wrap(~if_else(term %in% c('Male', 'Latino', 'White', 'Age 45', 'Age 60'), 'Demographics',
                      if_else(term %in% c('Construction worker', 'High school teacher', 
                                          'Local attorney', 'Not employed','Police officer',
                                          'Real estate developer', 'Previously elected to local office'), 'Experience',
                              if_else(term %in% c('Homeowner', 'Lived In Community 10 Years', 
                                                  'Married (two children)', 'Married (no children)'), 
                                      'Personal Life',
                                      'Endorsements'))),
             scales = 'free_y') +
  theme_bw() +
  theme(legend.position = 'bottom')

ggsave('fig_4.png', width = 8, height = 5)

## APPENDIX SECTION A.2.1: Conditional AMCES -----------------------------

## Figure A1: By respondent gender -------------------

amce_men <- lm_robust(fmla,
                      data = d |> filter(gender4 == 1),
                      cluster = caseid)

amce_women <- lm_robust(fmla,
                        data = d |> filter(gender4 == 2),
                        cluster = caseid)

modelplot(models = list('Men' = amce_men,
                        'Women' = amce_women),
          coef_omit = 'Inter', coef_map = cm) +
  scale_color_manual(values = c('black', 'red')) +
  geom_vline(xintercept = 0, linetype = 'dashed') +
  labs(x = 'AMCE', color = '') +
  # facet by variable group
  facet_wrap(~if_else(term %in% c('Male', 'Latino', 'White', 'Age 45', 'Age 60'), 'Demographics',
                      if_else(term %in% c('Construction worker', 'High school teacher', 
                                          'Local attorney', 'Not employed','Police officer',
                                          'Real estate developer', 'Previously elected to local office'), 'Experience',
                              if_else(term %in% c('Homeowner', 'Lived In Community 10 Years', 
                                                  'Married (two children)', 'Married (no children)'), 
                                      'Personal Life',
                                      'Endorsements'))),
             scales = 'free_y') +
  theme_bw() +
  theme(legend.position = 'bottom')

ggsave('fig_a1.png', width = 8, height = 5)

## Figure A2: By respondent race -------------------------------

amce_white <- lm_robust(fmla,
                        data = d |> filter(race == 1),
                        cluster = caseid)

amce_black <- lm_robust(fmla,
                        data = d |> filter(race == 2),
                        cluster = caseid)

amce_hispanic <- lm_robust(fmla,
                           data = d |> filter(race == 3),
                           cluster = caseid)

modelplot(models = list('White' = amce_white,
                        'Black' = amce_black,
                        'Hispanic' = amce_hispanic),
          coef_omit = 'Inter', coef_map = cm) +
  scale_color_manual(values = c('black', 'red', 'blue')) +
  geom_vline(xintercept = 0, linetype = 'dashed') +
  labs(x = 'AMCE', color = '') +
  # facet by variable group
  facet_wrap(~if_else(term %in% c('Male', 'Latino', 'White', 'Age 45', 'Age 60'), 'Demographics',
                      if_else(term %in% c('Construction worker', 'High school teacher', 
                                          'Local attorney', 'Not employed','Police officer',
                                          'Real estate developer', 'Previously elected to local office'), 'Experience',
                              if_else(term %in% c('Homeowner', 'Lived In Community 10 Years', 
                                                  'Married (two children)', 'Married (no children)'), 
                                      'Personal Life',
                                      'Endorsements'))),
             scales = 'free_y') +
  theme_bw() +
  theme(legend.position = 'bottom')

ggsave('fig_a2.png', width = 8, height = 5)

## Figure A3: By respondent's place of residence ------------------------

amce_city <- lm_robust(fmla,
                       data = d |> filter(urbancity == 1),
                       cluster = caseid)

amce_suburb <- lm_robust(fmla,
                         data = d |> filter(urbancity == 2),
                         cluster = caseid)

amce_town <- lm_robust(fmla,
                       data = d |> filter(urbancity == 3),
                       cluster = caseid)

amce_rural <- lm_robust(fmla,
                        data = d |> filter(urbancity == 4),
                        cluster = caseid)

modelplot(models = list('City' = amce_city,
                        'Suburb' = amce_suburb,
                        'Town' = amce_town,
                        'Rural' = amce_rural),
          coef_omit = 'Inter', coef_map = cm) +
  scale_color_brewer(palette = 'Set1') +
  geom_vline(xintercept = 0, linetype = 'dashed') +
  labs(x = 'AMCE', color = '') +
  # facet by variable group
  facet_wrap(~if_else(term %in% c('Male', 'Latino', 'White', 'Age 45', 'Age 60'), 'Demographics',
                      if_else(term %in% c('Construction worker', 'High school teacher', 
                                          'Local attorney', 'Not employed','Police officer',
                                          'Real estate developer', 'Previously elected to local office'), 'Experience',
                              if_else(term %in% c('Homeowner', 'Lived In Community 10 Years', 
                                                  'Married (two children)', 'Married (no children)'), 
                                      'Personal Life',
                                      'Endorsements'))),
             scales = 'free_y') +
  theme_bw() +
  theme(legend.position = 'bottom')

ggsave('fig_a3.png', width = 8, height = 5)


## Figure A4: By respondent's duration of residence ---------------------

# less than 1 year
amce_shortterm <- lm_robust(fmla,
                            data = d |> filter(residency %in% 1:3),
                            cluster = caseid)

# 1-5 years
amce_mediumterm <- lm_robust(fmla,
                             data = d |> filter(residency %in% 4:5),
                             cluster = caseid)

# 5+ years
amce_longterm <- lm_robust(fmla,
                           data = d |> filter(residency == 6),
                           cluster = caseid)

modelplot(models = list('< 1 year' = amce_shortterm,
                        '1-5 years' = amce_mediumterm,
                        '5+ years' = amce_longterm),
          coef_omit = 'Inter', coef_map = cm) +
  scale_color_manual(values = c('black', 'red', 'blue')) +
  geom_vline(xintercept = 0, linetype = 'dashed') +
  labs(x = 'AMCE', color = '') +
  # facet by variable group
  facet_wrap(~if_else(term %in% c('Male', 'Latino', 'White', 'Age 45', 'Age 60'), 'Demographics',
                      if_else(term %in% c('Construction worker', 'High school teacher', 
                                          'Local attorney', 'Not employed','Police officer',
                                          'Real estate developer', 'Previously elected to local office'), 'Experience',
                              if_else(term %in% c('Homeowner', 'Lived In Community 10 Years', 
                                                  'Married (two children)', 'Married (no children)'), 
                                      'Personal Life',
                                      'Endorsements'))),
             scales = 'free_y') +
  theme_bw() +
  theme(legend.position = 'bottom')

ggsave('fig_a4.png', width = 8, height = 5)

## APPENDIX SECTION A.2.3: Average Component Interaction Effects (ACIE) ---------

## A18: ACIE by respondent's age -------------------

fmla <- candidate_selected ~ Gender + Race + CareerHistory + 
  Age * Homeowner + Age * OldTimer + Political + Family + Endorsements

acie_age <- lm_robust(fmla,
                      data = d,
                      cluster = caseid)

mfx <- marginaleffects(acie_age,
                       variables = c('OldTimer', 'Homeowner'),
                       by = 'Age') |> 
  mutate(term = ifelse(term == 'OldTimer', 'Lived in Community 10 Years',
                       term))

ggplot(data = mfx,
       mapping = aes(x = estimate,
                     xmin = conf.low,
                     xmax = conf.high,
                     y = Age)) +
  geom_pointrange() +
  geom_vline(xintercept = 0, linetype = 'dashed') + 
  theme_bw() +
  labs(x = 'ACIE',
       color = '') +
  facet_wrap(~term)

ggsave('fig_a18.png', width = 8, height = 5)


## A19: ACIEs for each level of the Community Ties variable --------

# code Community Ties variable as a factor
d$CommunityTies <- factor(d$CommunityTies) |> 
  fct_relevel('Has rented an apartment in your community for the past 2 years.')

fmla <- candidate_selected ~ Gender + Race + CareerHistory + 
  Age + CommunityTies + Political + Family + Endorsements

acie_community_ties <- lm_robust(fmla,
                                 data = d,
                                 cluster = caseid)

acie_community_ties |> 
  tidy() |> 
  filter(str_detect(term, 'CommunityTies')) |> 
  mutate(term = str_remove_all(term, 'CommunityTies')) |> 
  ggplot(mapping = aes(x = estimate,
                       xmin = conf.low,
                       xmax = conf.high,
                       y = term)) +
  geom_pointrange() +
  theme_bw() +
  labs(x = 'ACIE',
       y = '') +
  geom_vline(xintercept = 0, linetype = 'dashed')

ggsave('fig_a19.png', width = 8, height = 5)

## APPENDIX SECTION A.2.2: Conditional Marginal Means -------------

## Load and clean dataset -----------------------

rm(list=ls())
load('CES2022_UGA.RData')

# to estimate AMCE with linear regression, make the candidate the unit of analysis
d <- d |> 
  mutate(contest_id = 1:n())

candidate1 <- d |> 
  mutate(candidate_selected = as.numeric(conjoint_choice == 1)) |> 
  select(caseid, contest_id, teamweight, candidate_selected, 
         Age = A_Age, CareerHistory = A_CareerHistory,
         CommunityTies = A_CommunityTies, Name = A_Name,
         Race = A_Race, Gender = A_Gender, Political = A_Political,
         Endorsements = A_Endorsements, Family = A_Family,
         pid3, race, urbancity, residency = CC22_361, gender4, ownhome)

candidate2 <- d |> 
  mutate(candidate_selected = as.numeric(conjoint_choice == 2)) |> 
  select(caseid, contest_id, teamweight, candidate_selected, 
         Age = B_Age, CareerHistory = B_CareerHistory,
         CommunityTies = B_CommunityTies, Name = B_Name,
         Race = B_Race, Gender = B_Gender, Political = B_Political,
         Endorsements = B_Endorsements, Family = B_Family,
         pid3, race, urbancity, residency = CC22_361, gender4, ownhome)

d <- bind_rows(candidate1, candidate2)

# convert attribute variables to factors
d <- d |> 
  mutate(Homeowner = factor(ifelse(str_detect(CommunityTies, 'owned a home'),
                                   'Homeowner', 'Renter'),
                            levels = c('Renter', 'Homeowner')),
         OldTimer = factor(ifelse(str_detect(CommunityTies, 'past 10 years'), 
                                  'Lived in community 10 years',
                                  'Lived in community 2 years'),
                           levels = c('Lived in community 2 years',
                                      'Lived in community 10 years')),
         # make "no endorsements" the base category
         Endorsements = case_when(str_detect(Endorsements, 'real estate') ~ 'Assn of real estate developers',
                                  str_detect(Endorsements, 'commerce') ~ 'Chamber of commerce',
                                  TRUE ~ Endorsements) |> 
           factor() |> 
           fct_relevel('No endorsements'),
         # make "single" the base category for family
         Family = factor(Family) |> 
           fct_relevel('Single'),
         Age = factor(case_when(Age == '60' ~ 'Age 60',
                                Age == '45' ~ 'Age 45',
                                Age == '30' ~ 'Age 30')),
         CareerHistory = case_when(str_detect(CareerHistory, 'Not employed') ~ 'Not employed',
                                   TRUE ~ CareerHistory) |> 
           factor(),
         Political = factor(Political),
         Family = factor(Family),
         Gender = factor(Gender),
         Race = factor(case_when(Race == 'B' ~ 'Black',
                                 Race == 'L' ~ 'Latino',
                                 Race == 'W' ~ 'White')))

## Figure A5: Marginal Means --------------------

fmla <- candidate_selected ~ Gender + Race + CareerHistory + 
  Homeowner + OldTimer + Age + Political + Family + Endorsements

out <- mm(data = d,
          formula = fmla,
          id = ~caseid,
          h0 = 0.5)

out |>  
  # mutate(level = fct_reorder(level, estimate)) |> 
  ggplot(
    mapping = aes(x=estimate, 
                  xmax=upper,
                  xmin=lower,
                  y=level)) +
  geom_pointrange() +
  geom_vline(xintercept = 0.5, linetype = 'dashed') +
  facet_wrap(~if_else(level %in% c('Male', 'Female', 'Latino', 'White', 'Black', 'Age 30', 'Age 45', 'Age 60'), 'Demographics',
                      if_else(level %in% c('Construction worker', 'High school teacher', 
                                           'Local attorney', 'Not employed','Police officer',
                                           'Real estate developer', 'Business owner',
                                           'Previously elected to local office',
                                           'No previous political experience'), 'Experience',
                              if_else(level %in% c('Homeowner', 'Renter', 'Lived in community 2 years',
                                                   'Lived in community 10 years', 'Single',
                                                   'Married with two children', 'Married with no children'), 
                                      'Personal Life',
                                      'Endorsements'))),
             scales = 'free_y') +
  theme_bw() +
  labs(x = 'Marginal Means', y = 'Attribute')

ggsave('fig_a5.png', width = 8, height = 5)

## Functions to estimate and plot subgroup marginal means -----------------

subgroup_mm <- function(d, groupvar, weights = NULL){
  
  fmla <- candidate_selected ~ Gender + Race + CareerHistory + 
    Homeowner + OldTimer + Age + Political + Family + Endorsements
  
  # split the dataframe by grouping variable
  d_split <- split(d, d[[groupvar]])
  
  # estimate marginal means for each respondent subgroup
  mm <- lapply(d_split,
               mm, 
               formula = fmla,
               id = ~caseid,
               weights = weights,
               h0 = 0.5)
  
  # bind into a single dataframe with a 'subgroup' column and return
  mm |> 
    map_df(~ .x, .id = "subgroup")
}

# plot
plot_subgroup_mm <- function(mm, color_scheme = c('black', 'red', 'blue')){
  ggplot(
    data = mm,
    mapping = aes(x=estimate, 
                  xmax=upper,
                  xmin=lower,
                  y=level,
                  color = subgroup)) +
    geom_pointrange(position = position_dodge(0.5)) +
    geom_vline(xintercept = 0.5, linetype = 'dashed') +
    facet_wrap(~if_else(level %in% c('Male', 'Female', 'Latino', 'White', 'Black', 'Age 30', 'Age 45', 'Age 60'), 'Demographics',
                        if_else(level %in% c('Construction worker', 'High school teacher', 
                                             'Local attorney', 'Not employed','Police officer',
                                             'Real estate developer', 'Business owner',
                                             'Previously elected to local office',
                                             'No previous political experience'), 'Experience',
                                if_else(level %in% c('Homeowner', 'Renter', 'Lived in community 2 years',
                                                     'Lived in community 10 years', 'Single',
                                                     'Married with two children', 'Married with no children'), 
                                        'Personal Life',
                                        'Endorsements'))),
               scales = 'free_y') +
    theme_bw() +
    labs(x = 'Marginal Means', y = 'Attribute', color = '') +
    theme(legend.position = 'bottom') +
    scale_color_manual(values = color_scheme)
}


## Figure A6: Marginal Means by respondent's party --------------------

d$party <- factor(
  case_when(d$pid3 == 1 ~ 'Democratic',
            d$pid3 == 2 ~ 'Republican'))

mm <- subgroup_mm(d, 'party')

democratic_blue <- "#2E74C0"
republican_red <- "#CB454A"

plot_subgroup_mm(mm, c(democratic_blue, republican_red))

ggsave('fig_a6.png', width = 8, height = 5)

## Figure A7: Subgroup Marginal Means - Homeownership --------------------

d$ownhome <- factor(
  case_when(d$ownhome == 1 ~ 'Owners',
            d$ownhome == 2 ~ 'Renters'))

mm <- subgroup_mm(d, 'ownhome')

plot_subgroup_mm(mm)

ggsave('fig_a7.png', width = 8, height = 5)


## Figure A8: Subgroup Marginal Means - Gender --------------------

d$respondent_gender <- factor(
  case_when(d$gender4 == 1 ~ 'Men',
            d$gender4 == 2 ~ 'Women'))

mm <- subgroup_mm(d, 'respondent_gender')

plot_subgroup_mm(mm)

ggsave('fig_a8.png', width = 8, height = 5)

## Figure A9: Subgroup Marginal Means - Race --------------------

d$respondent_race <- factor(
  case_when(d$race == 1 ~ 'White',
            d$race == 2 ~ 'Black',
            d$race == 3 ~ 'Hispanic'))

mm <- subgroup_mm(d, 'respondent_race')

plot_subgroup_mm(mm)

ggsave('fig_a9.png', width = 8, height = 5)

## Figure A10: Subgroup Marginal Means - Place of Residence --------------------

d$urbancity <- factor(
  case_when(d$urbancity == 1 ~ 'City',
            d$urbancity == 2 ~ 'Suburb',
            d$urbancity == 3 ~ 'Town',
            d$urbancity == 4 ~ 'Rural'))

mm <- subgroup_mm(d, 'urbancity')

plot_subgroup_mm(mm, c( '#58508d', '#bc5090', '#ff6361', '#ffa600')) # '#003f5c',

ggsave('fig_a10.png', width = 8, height = 5)

## Figure A11: Subgroup Marginal Means - Residency Duration --------------------

d$duration <- factor(
  case_when(d$residency %in% 1:3 ~ 'Less Than 1 Year',
            d$residency %in% 4:5 ~ '1-5 Years',
            d$residency == 6 ~ 'More Than 5 Years')) |> 
  fct_relevel('Less Than 1 Year')

mm <- subgroup_mm(d, 'duration') |> 
  mutate(subgroup = fct_relevel(subgroup, 'Less Than 1 Year'))

plot_subgroup_mm(mm)

ggsave('fig_a11.png', width = 8, height = 5)

## Figures A12-A17: Differences in Marginal Means -------------

plot_mm_diffs <- function(out){
  ggplot(
    data = out,
    mapping = aes(x=estimate, 
                  xmax=upper,
                  xmin=lower,
                  y=level,
                  color=BY)) +
    geom_pointrange(position = position_dodge(0.5)) +
    geom_vline(xintercept = 0, linetype = 'dashed') +
    facet_wrap(~if_else(level %in% c('Male', 'Female', 'Latino', 'White', 'Black', 'Age 30', 'Age 45', 'Age 60'), 'Demographics',
                        if_else(level %in% c('Construction worker', 'High school teacher', 
                                             'Local attorney', 'Not employed','Police officer',
                                             'Real estate developer', 'Business owner',
                                             'Previously elected to local office',
                                             'No previous political experience'), 'Experience',
                                if_else(level %in% c('Homeowner', 'Renter', 'Lived in community 2 years',
                                                     'Lived in community 10 years', 'Single',
                                                     'Married with two children', 'Married with no children'), 
                                        'Personal Life',
                                        'Endorsements'))),
               scales = 'free_y') +
    theme_bw() +
    labs(x = 'Difference in Marginal Means', y = 'Attribute', color = '') +
    theme(legend.position = 'bottom') +
    scale_color_manual(values = c('black', 'red', 'blue'))
}

# Figure A12: party
mm_diffs(data = d,
         formula = fmla,
         id = ~caseid,
         by = ~party,
         h0 = 0) |> 
  plot_mm_diffs()

ggsave('fig_a12.png', width = 8, height = 5)

# Figure A13: homeownership
mm_diffs(data = d,
         formula = fmla,
         id = ~caseid,
         by = ~ownhome,
         h0 = 0) |> 
  plot_mm_diffs()

ggsave('fig_a13.png', width = 8, height = 5)

# Figure A14: gender
mm_diffs(data = d,
         formula = fmla,
         id = ~caseid,
         by = ~respondent_gender,
         h0 = 0) |> 
  plot_mm_diffs()

ggsave('fig_a14.png', width = 8, height = 5)

# Figure A15: race
mm_diffs(data = d,
         formula = fmla,
         id = ~caseid,
         by = ~respondent_race,
         h0 = 0) |> 
  plot_mm_diffs()

ggsave('fig_a15.png', width = 8, height = 5)

# Figure A16: place of residency
mm_diffs(data = d,
         formula = fmla,
         id = ~caseid,
         by = ~urbancity,
         h0 = 0) |> 
  plot_mm_diffs()

ggsave('fig_a16.png', width = 8, height = 5)

# Figure A17: duration of residency
mm_diffs(data = d,
         formula = fmla,
         id = ~caseid,
         by = ~duration,
         h0 = 0) |> 
  plot_mm_diffs()

ggsave('fig_a17.png', width = 8, height = 5)

## APPENDIX SECTION A.2.4: Nationally Representative Subsample -------

d_matched <- d |> 
  filter(!is.na(teamweight)) |> 
  # category variable for unconditional marginal mean estimation
  mutate(all = 'All Respondents')

fmla <- candidate_selected ~ Gender + Race + CareerHistory + 
  Homeowner + OldTimer + Age + Political + Family + Endorsements

# Figure A20: all respondents
mm <- subgroup_mm(d_matched, 'all', weights = ~teamweight)
plot_subgroup_mm(mm, 'black')

ggsave('fig_a20.png', width = 8, height = 5)

# Figure A21: by party
mm <- subgroup_mm(d_matched, 'party', weights = ~teamweight)
plot_subgroup_mm(mm, c(democratic_blue, republican_red))

ggsave('fig_a21.png', width = 8, height = 5)

# Figure A22: by homeownership
mm <- subgroup_mm(d_matched, 'ownhome', weights = ~teamweight)
plot_subgroup_mm(mm)

ggsave('fig_a22.png', width = 8, height = 5)

# Figure A23: by gender
mm <- subgroup_mm(d_matched, 'respondent_gender', weights = ~teamweight)
plot_subgroup_mm(mm)

ggsave('fig_a23.png', width = 8, height = 5)

# Figure A24: by race
mm <- subgroup_mm(d_matched, 'respondent_race', weights = ~teamweight)
plot_subgroup_mm(mm)

ggsave('fig_a24.png', width = 8, height = 5)

# Figure A25: by place of residence
mm <- subgroup_mm(d_matched, 'urbancity', weights = ~teamweight)
plot_subgroup_mm(mm, c( '#58508d', '#bc5090', '#ff6361', '#ffa600'))

ggsave('fig_a25.png', width = 8, height = 5)

# Figure A26: by residency duration
mm <- subgroup_mm(d_matched, 'duration', weights = ~teamweight) |> 
  mutate(subgroup = fct_relevel(subgroup, 'Less Than 1 Year'))

plot_subgroup_mm(mm)

ggsave('fig_a26.png', width = 8, height = 5)

